package com.ruoyi.doc.controller;

import java.io.File;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.doc.domain.SysDoc;
import com.ruoyi.doc.service.ISysDocService;
import com.ruoyi.doc.util.RyDocUtil;
import com.ruoyi.doc.util.RyFileUtil;
import com.ruoyi.framework.config.ServerConfig;

/**
 * 文档模板Controller
 * 
 * @author ruoyi
 * @date 2021-01-09
 */
@RestController
@RequestMapping("/doc/doc")
public class SysDocController extends BaseController
{
    @Autowired
    private ISysDocService sysDocService;
    @Autowired
    private ServerConfig serverConfig;

    /**
     * 查询文档模板列表
     */
    @PreAuthorize("@ss.hasPermi('doc:doc:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysDoc sysDoc)
    {
        startPage();
        List<SysDoc> list = sysDocService.selectSysDocList(sysDoc);
        return getDataTable(list);
    }

    /**
     * 导出文档模板列表
     */
    @PreAuthorize("@ss.hasPermi('doc:doc:export')")
    @Log(title = "文档模板", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(SysDoc sysDoc)
    {
        List<SysDoc> list = sysDocService.selectSysDocList(sysDoc);
        ExcelUtil<SysDoc> util = new ExcelUtil<SysDoc>(SysDoc.class);
        return util.exportExcel(list, "doc");
    }

    /**
     * 获取文档模板详细信息
     */
    @PreAuthorize("@ss.hasPermi('doc:doc:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(sysDocService.selectSysDocById(id));
    }

    /**
     * 新增文档模板
     * @throws Exception 
     */
    @PreAuthorize("@ss.hasPermi('doc:doc:add')")
    @Log(title = "文档模板", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysDoc sysDoc) throws Exception
    {
        return toAjax(sysDocService.insertSysDoc(sysDoc));
    }

    /**
     * 修改文档模板
     * @throws Exception 
     */
    @PreAuthorize("@ss.hasPermi('doc:doc:edit')")
    @Log(title = "文档模板", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysDoc sysDoc) throws Exception
    {
        return toAjax(sysDocService.updateSysDoc(sysDoc));
    }

    /**
     * 删除文档模板
     */
    @PreAuthorize("@ss.hasPermi('doc:doc:remove')")
    @Log(title = "文档模板", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(sysDocService.deleteSysDocByIds(ids));
    }
    
    /**
     * 预览
     * @throws Exception 
     */
    @PreAuthorize("@ss.hasPermi('doc:doc:remove')")
    @Log(title = "预览", businessType = BusinessType.EXPORT)
    @GetMapping("/preview/{ids}")
    public AjaxResult preview(@PathVariable Long[] ids) throws Exception
    {
    	// 1、查询表数据
		SysDoc doc = sysDocService.selectSysDocById(ids[0]);
		
		// 文档类型
		File file = RyDocUtil.getPdfFile(doc.getDocxml());
		
        // 上传文件路径
        String filePath = RuoYiConfig.getUploadPath();
        // 上传并返回新文件名称
        String fileName = FileUploadUtils.upload(filePath, RyFileUtil.getMulFileByPath(file.getAbsolutePath()));
        String url = serverConfig.getUrl() + fileName;
		return AjaxResult.success(url);
    	
    }
    
}
